﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;
using Red.Core.Extensions;

namespace Red.Launcher.Diagnostics.Process
{
    public class Definition
    {
        #region Public Properties

        public string Name { get; set; }
        public bool Ignore { get; set; }
        public bool IncludeChildren { get; set; }

        public IList<Definition> Parents { get; private set; } = new List<Definition>();
        public IList<Definition> Collection { get; private set; } = new List<Definition>();

        #endregion Public Properties

        #region Public Methods

        public static Definition FromXml( XElement element )
        {
            Debug.Assert( element.Name == "process" );

            Definition definition = new Definition()
            {
                Name = element.GetAttribute<string>( "name" ),
                Ignore = element.GetAttribute<bool>( "ignore" ),
                IncludeChildren = element.GetAttribute<bool>( "children" )
            };

            XElement parents = element.Element( "parents" );
            AddSubDefinition( definition.Parents, parents );

            XElement collected = element.Element( "collection" );
            AddSubDefinition( definition.Collection, collected );

            return definition;
        }

        #endregion Public Methods

        #region Private Methods

        private static void AddSubDefinition( IList<Definition> list, XElement source )
        {
            if ( source != null )
            {
                foreach ( XElement childElement in source.Elements() )
                {
                    Definition child = FromXml( childElement );
                    list.Add( child );
                }
            }
        }

        #endregion Private Methods
    }
}
